package com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel

import androidx.databinding.ObservableField
import androidx.lifecycle.ViewModel
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.async
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.StudyAnalysisInfo
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.repo.StudyAnalysisRepo
import java.math.BigDecimal

/**
 * Created by Dadong on 2021/1/26
 * Describe:
 */
public class StudyAnalysisViewModel constructor(private var repo: StudyAnalysisRepo) : ViewModel() {
    var title: ObservableField<String> = ObservableField<String>()
    val isOpen: ObservableField<Boolean> = ObservableField<Boolean>()
    var studyAnalysisInfo: ObservableField<StudyAnalysisInfo> = ObservableField<StudyAnalysisInfo>()
    var weekStudyAnalysisInfo = ObservableField<StudyAnalysisInfo>()
    var monthStudyAnalysisInfo = ObservableField<StudyAnalysisInfo>()
    var isWeek: ObservableField<Boolean> = ObservableField(true)
    var count = ObservableField<String>()
    var maxItem = ObservableField<String>()
    var minItem = ObservableField<String>()

    fun getStatisticData(): MutableList<String> {
        var statisticData = ArrayList<String>()
        statisticData.add(studyAnalysisInfo.get()!!.LearingAnalysisClass.ClassCompleteNum.toString())
        statisticData.add("${(studyAnalysisInfo.get()!!.LearingAnalysisClass.ClassRate * 100)
                .toBigDecimal().setScale(0, BigDecimal.ROUND_HALF_UP)}%")
        statisticData.add(studyAnalysisInfo.get()!!.LearingAnalysisClass.ClassRank.toString())
        statisticData.add("${(studyAnalysisInfo.get()!!.LearingAnalysisClass.ClassPaperSubRate * 100)
                .toBigDecimal().setScale(0, BigDecimal.ROUND_HALF_UP)}%")
        statisticData.add("${(studyAnalysisInfo.get()!!.LearingAnalysisClass.ClassExcellentRate * 100)
                .toBigDecimal().setScale(0, BigDecimal.ROUND_HALF_UP)}%")
        statisticData.add("${(studyAnalysisInfo.get()!!.LearingAnalysisClass.ClassStandardRate * 100)
                .toBigDecimal().setScale(0, BigDecimal.ROUND_HALF_UP)}%")
        return statisticData
    }

    /*
   * 是否开启班级学情分析 接口
   * */
    fun isOpenClassLearningAnalysisApp() =
            repo.isOpenClassLearningAnalysisApp()
                    .async()
                    .doOnSuccess {
                        isOpen.set(it.data)
                    }

    /*
  * 获取班级学情分析报告
  * */
    fun getClassLearningAnalysis() =
            repo.getClassLearningAnalysis(-1)
                    .async()
                    .doOnSuccess { it ->
                        studyAnalysisInfo.set(it.data)
                        count.set(it.data.PaperAnswerDetail.TotalPaperCount.toString())
                        var maxItemString = ""
                        it.data.QuesTypeAnalysis.MaxRateGenreName.forEachIndexed { index, s ->
                            if (index <= 1) {
                                maxItemString += "$s "
                            }
                        }
                        if (it.data.QuesTypeAnalysis.MaxRateGenreName.size > 2) {
                            maxItemString += "...等"
                        }
                        if (maxItemString.isEmpty()) {
                            maxItemString = "无"
                        }
                        maxItem.set(maxItemString)
                        var minItemString = ""
                        it.data.QuesTypeAnalysis.MinRateGenreName.forEachIndexed { index, s ->
                            if (index <= 1)
                                minItemString += "$s "
                        }
                        if (it.data.QuesTypeAnalysis.MinRateGenreName.size > 2) {
                            minItemString += "...等"
                        }
                        if (minItemString.isEmpty()) {
                            minItemString = "无"
                        }
                        minItem.set(minItemString)
                        title.set(it.data.LearingAnalysisClass.CourseClassName + "的学情分析")
                    }

    /**
     * 获取一周学情分析报告
     */
    fun getWeekClassLearningAnalysis() =
            repo.getClassLearningAnalysis(7)
                    .async()
                    .doOnSuccess {
                        weekStudyAnalysisInfo.set(it.data)
                    }

    /**
     * 获取一个月的学情分析报告
     */
    fun getMonthClassLearningAnalysis() =
            repo.getClassLearningAnalysis(30)
                    .async()
                    .doOnSuccess {
                        monthStudyAnalysisInfo.set(it.data)
                    }
}